<?php
//es测试案例
use Elasticsearch\ClientBuilder;
class ESTestController extends Yaf\Controller_Abstract{

    private $client;
    public function init(){
        Yaf\Dispatcher::getInstance()->disableView();
    //$this->client = ClientBuilder::create()->build();
        $hosts = [
            '127.0.0.1:9200', //IP+端口
        ];
        $this->client = ClientBuilder::create()->setHosts($hosts)->build();
        }
    //创建索引
    public function createIndexAction(){
        $params = [
            'index'=>'article',
            'body'=>[
                'settings'=>[
                    'number_of_shards'=>3,
                    'number_of_replicas'=>2
                ],
                'mappings'=>[
                    '_source'=>[
                      'enabled'=>true
                    ],
                'properties'=>[
                    'title'=>[
                          'type'=>'text',
                          'analyzer'=>'ik_max_word',
                        ],
                    'content'=>[
                          'type'=>'text',
                          'analyzer'=>'ik_max_word',
                        ],
                    'tags'=>[
                          'type'=>'text',
                          'analyzer'=>'ik_max_word',
                        ],
                    'status'=>[
                          'type'=>'text'
                        ],
                    'uid'=>[
                          'type'=>'text'
                        ],
                    'updated_at'=>[
                        'type'=>'text'
                    ],
                    ]
                ]
            ]
        ];

        $this->client->indices()->create($params);

    }
//    插入操作
    public function addManageAction(){
        $articles = ArticleModel::all();

        foreach ($articles as $key=>$document){
            $params['body'][]=[
               'index'=>[
                   '_index'=>'article',
                   '_id'=>$key
               ]
            ];
            $params['body'][]=[
              'title'=>$document['title'],
              'content'=>$document['content'],
              'tags'=>$document['tags'],
              'status'=>$document['status'],
              'uid'=>$document['uid'],
              'updated_at'=>$document['updated_at'],
            ];
        }
        if (isset($params) && !empty($params)){
            $this->client->bulk($params);
        }
    }
//获取操作
    public function selectManageAction(){
        $params =[
            'index'=>'article',
            'id'=>1
        ];
        $response = $this->client->get($params);
        var_dump(
            $response
        );
    }
//    分页获取
    public function selectGAction(){
        $params=[
          'scroll'=>'30s',
            'size'=>4,
            'index'=>'article',
            'body'=>[
                'query'=>[
                    'match_all'=>new \stdClass(),
                ]
            ]
        ];
        $response = $this->client->search($params);
        var_dump(
            $response
        );
    }
//    字段匹配
    public function filesMatchAction(){
        $keywords = $_POST['Kwords'];
        $params = [
            'size'=>3,
            'index'=>'article',
            'body'=>[
               'query' => [
                   'multi_match' => [
                       'query' => $keywords,
                       'fields' => ['title','content','tags']
                   ]
               ],
            ]
        ];
        $response=$this->client->search($params);
        echo json_encode($response['hits']['hits'],JSON_UNESCAPED_UNICODE );
    }

}